package com.accompany.service.impl;

import com.accompany.entity.ChatMessage;
import com.accompany.dao.ChatMessageMapper;
import com.accompany.service.ChatMessageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 聊天消息表 服务实现类
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@Slf4j
@Service
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageMapper, ChatMessage> implements ChatMessageService {

    @Autowired
    private ChatMessageMapper chatMessageMapper;

    /**
     * 分页查询聊天消息列表
     *
     * @param page 页码
     * @param size 每页大小
     * @return 分页结果
     */
    @Override
    public Page<ChatMessage> getPaginatedChatMessages(Integer page, Integer size) {
        try {
            log.info("分页查询聊天消息列表，页码：{}, 每页大小：{}", page, size);
            Page<ChatMessage> pageObj = new Page<>(page, size);
            Page<ChatMessage> chatMessagePage = chatMessageMapper.selectPage(pageObj, null);
            // 获取分页结果中的记录列表并遍历打印
            chatMessagePage.getRecords().forEach(item -> log.info(item.toString()));
            return chatMessagePage;
        } catch (Exception e) {
            log.error("分页查询聊天消息列表时发生错误，页码：{}, 每页大小：{}", page, size, e);
            throw e;
        }
    }

    /**
     * 获取所有聊天消息列表
     *
     * @return 聊天消息列表
     */
    @Override
    public List<ChatMessage> getAllChatMessages() {
        try {
            log.info("获取所有聊天消息列表");
            return chatMessageMapper.selectList(null);
        } catch (Exception e) {
            log.error("获取所有聊天消息列表时发生错误", e);
            throw e;
        }
    }

    /**
     * 获取单个聊天消息信息
     *
     * @param id 聊天消息ID
     * @return 聊天消息对象
     */
    @Override
    public ChatMessage getChatMessageById(Long id) {
        try {
            log.info("获取单个聊天消息信息，聊天消息ID：{}", id);
            return chatMessageMapper.selectById(id);
        } catch (Exception e) {
            log.error("获取单个聊天消息信息时发生错误，聊天消息ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 更新聊天消息信息
     *
     * @param chatMessage 聊天消息信息
     */
    @Override
    @Transactional
    public void updateChatMessage(ChatMessage chatMessage) {
        try {
            log.info("更新聊天消息信息，聊天消息信息：{}", chatMessage.toString());
            chatMessageMapper.updateById(chatMessage);
        } catch (Exception e) {
            log.error("更新聊天消息信息时发生错误，聊天消息信息：{}", chatMessage.toString(), e);
            throw e;
        }
    }

    /**
     * 删除聊天消息
     *
     * @param id 聊天消息ID
     */
    @Override
    @Transactional
    public void deleteChatMessage(Long id) {
        try {
            log.info("删除聊天消息，聊天消息ID：{}", id);
            chatMessageMapper.deleteById(id);
        } catch (Exception e) {
            log.error("删除聊天消息时发生错误，聊天消息ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 创建聊天消息
     *
     * @param chatMessage 聊天消息信息
     */
    @Override
    @Transactional
    public void createChatMessage(ChatMessage chatMessage) {
        try {
            log.info("创建聊天消息：{}", chatMessage.toString());
            chatMessageMapper.insert(chatMessage);
        } catch (Exception e) {
            log.error("创建聊天消息时发生错误，聊天消息信息：{}", chatMessage.toString(), e);
            throw e;
        }
    }
}